package com.charm.codes.nethard.interceptors;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * 为okhttp添加缓存，这里是考虑到服务器不支持缓存时，从而让okhttp支持缓存
 *
 * @author tanrui
 * @date 2017/3/2
 */

public class CacheInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        return null;
    }

//    @Override
//    public Response intercept(Chain chain) throws IOException {
//
//        // 有网络时 设置缓存超时时间3分钟
//        int maxAge = 3 * 60;
//        // 无网络时，设置超时为一个小时
//        int maxStale = 60 * 60;
//        Request request = chain.request();
//        if (AppNetworkMgr.isNetworkConnected(FKApplication.getInstance())) {
//            //有网络时只从网络获取
//            request = request.newBuilder()
//                    .cacheControl(CacheControl.FORCE_NETWORK)
//                    .build();
//        } else {
//            //无网络时只从缓存中读取
//            request = request.newBuilder()
//                    .cacheControl(CacheControl.FORCE_CACHE)
//                    .build();
//        }
//        Response response = chain.proceed(request);
//        if (AppNetworkMgr.isNetworkConnected(FKApplication.getInstance())) {
//            response = response.newBuilder()
//                    .removeHeader("Pragma")
//                    .header("Cache-Control", "public, max-age=" + maxAge)
//                    .build();
//        } else {
//            response = response.newBuilder()
//                    .removeHeader("Pragma")
//                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//                    .build();
//        }
//        return response;
//    }
}
